//
//  M1ColumnViewController.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/19.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1ColumnViewController.h"
#import "M1ColumnListCell.h"
#import "M1ColumnModel.h"
#import "M1ColumnDetailsVC.h"
#import "MJRefresh.h"

@interface M1ColumnViewController ()

@end

@implementation M1ColumnViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.m1_hiddenNav = YES;
    
    self.m1_listData = [NSMutableArray arrayWithCapacity:0];
    
    self.m1_tableView.backgroundColor = m1_color(0xF6F8FB);
    
    [self.m1_tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(STATUS_HEIGHT, 0, 0, 0));
    }];
    
    [self.m1_tableView registerClass:[M1ColumnListCell class] forCellReuseIdentifier:m1_tableViewCellIdentifier];
    
    WEAKSELF
    self.m1_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf m1_requestData];
    }];
    
    [self m1_requestData];
}

- (void)m1_requestData{
    WEAKSELF
    [M1HttpNet requestWithMethod:m1_prefecture_list_url params:nil success:^(id resDic, NSInteger code, NSString *message) {
        if ([resDic count] > 0) {
            [weakSelf.m1_listData removeAllObjects];
            [weakSelf.m1_listData addObjectsFromArray:resDic[@"prefecture"]];
            [weakSelf.m1_tableView reloadData];            
        }
        [weakSelf.m1_tableView.mj_header endRefreshing];
    } fail:^(id resDic, NSInteger code, NSString *message) {
        [weakSelf.m1_tableView.mj_header endRefreshing];
    } showAni:YES];
}

#pragma mark - tableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (![M1User user]) {
        WEAKSELF
        [M1Alert m1_alertWithTitle:nil message:m1_loginText buttonTitles:@[@"取消", @"登录"] action:^(NSInteger index) {
            if (index == 1) {
                M1LoginVC *loginVC = [[M1LoginVC alloc] init];
                loginVC.m1_changeNavTitleColor = YES;
                loginVC.m1_changeBackImage = YES;
                loginVC.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:loginVC animated:YES];
            }
        }];
        return;
    }
    
    M1ColumnModel *model = [M1ColumnModel mj_objectWithKeyValues:self.m1_listData[indexPath.row]];

    M1ColumnDetailsVC *detailsVC = [[M1ColumnDetailsVC alloc] init];
    detailsVC.hidesBottomBarWhenPushed = YES;
    detailsVC.m1_model = model;
    [self.navigationController pushViewController:detailsVC animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.m1_listData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 168 * SCREENWIDTH / 375 + 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    M1ColumnListCell *cell = [tableView dequeueReusableCellWithIdentifier:m1_tableViewCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    M1ColumnModel *model = [M1ColumnModel mj_objectWithKeyValues:self.m1_listData[indexPath.row]];
    
    cell.m1_model = model;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
